/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.starwifi.duobao;


import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

import java.io.File;

/**
 * Utils - 系统设置
 * 
 * @author ruoo
 * @version 3.0
 */
@SuppressWarnings("unchecked")
public final class SettingUtils {

	/** 日期格式配比 */
	public static final String[] DATE_PATTERNS = new String[] { "yyyy", "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyy-MM-dd", "yyyyMMdd", "yyyy/MM/dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss" };


	/** 缓存名称 */
	public static final String CACHE_NAME = "setting";

	/** 缓存Key */
	public static final Integer CACHE_KEY = 0;


	/** CacheManager */
	private static final CacheManager cacheManager = CacheManager.create();



	public static String configPath="/app.xml";

	/**
	 * 不可实例化
	 */
	private SettingUtils() {
	}

	/**
	 * 获取系统设置
	 * 
	 * @return 系统设置
	 */
	public static String get(String key) {
		Ehcache cache = cacheManager.getEhcache(CACHE_NAME);
		net.sf.ehcache.Element cacheElement = cache.get(CACHE_KEY);
		String result="";
		if (cacheElement != null) {
			result = (String) cacheElement.getObjectValue();
		} else {
			try {
				File xmlFile = new ClassPathResource("/app.xml").getFile();
				Document document = new SAXReader().read(xmlFile);
				org.dom4j.Element element=(org.dom4j.Element)document.selectSingleNode("/app/setting[@key='"+key+"']");
				if(element!=null){
					result=element.attributeValue("value");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			cache.put(new net.sf.ehcache.Element(key, result));
		}
		return result;
	}


}